from docker import login, pull_image, create_image_tag, push_image, create_manifest, inspect_manifest, push_manifest, \
    remove_manifest, remove_image
from model import Inputs, get_origin_digest_dict, create_target_registry_token, check_target_digest_existence

inputs = Inputs()

origin_digest_dict = get_origin_digest_dict(inputs)

login(inputs.origin_registry_username, inputs.origin_registry_password, inputs.origin_registry_address)
login(inputs.target_registry_username, inputs.target_registry_password, inputs.target_registry_address)

target_registry_token = create_target_registry_token(inputs)
target_image = inputs.get_target_image()
temp_images = []
for os_arch in origin_digest_dict.keys():
    digest = origin_digest_dict[os_arch]
    temp_images.append(inputs.get_temp_image(digest))
    if check_target_digest_existence(inputs, target_registry_token, os_arch, digest):
        continue

    origin_image = inputs.get_origin_image(digest)

    pull_image(origin_image)
    create_image_tag(origin_image, target_image)
    push_image(target_image)

    remove_image(target_image)
    # remove_image(origin_image)

create_manifest(target_image, temp_images, True)

inspect_manifest(target_image)

push_manifest(target_image, True)

remove_manifest(target_image)
